package com.pur.zsms.weixinpay.weixinPayCaddemo;

import java.net.ConnectException;
import java.util.Map;
import java.util.TreeMap;

import com.pur.zsms.weixinpay.weixinPayCaddemo.dao.PayDao;
import com.pur.zsms.weixinpay.weixinPayCaddemo.dao.QueryDao;
import com.pur.zsms.weixinpay.weixinPayCaddemo.utils.HttpUtil;
import com.pur.zsms.weixinpay.weixinPayCaddemo.utils.XMLUtil;

public class wechatscan {

	public static String appid = "";
	public static String sub_appid = "";
	public static String mch_id = "";

	public static String device_info = "";

	public static String nonce_str = "";

	public static String sign = "";// 签名
	// public static String sign_type = ""; //默认MD5
	public static String body = "";
	public static String detail = "";
	public static String attach = "";

	public static String out_trade_no = "";// 日期+4位随机数

	public static String total_fee = "";
	public static String fee_type = "";
	public static String spbill_create_ip = "";
	public static String goods_tag = "";
	public static String auth_code = "";
	public static String limit_pay = "";
	public static String sub_mch_id = "";
	public static String key = "zzyZzy26262415ZzyzzyzzyZzy262624";

	public static boolean scanpay(PayDao pd) throws Exception {

		appid = "wx64bdfb1d6682c965";
		sub_appid = pd.getSub_appid() == null ? "" : pd.getSub_appid();
		mch_id = "1450773502";// 商户号

		device_info = "195";// shopId

		String currTime = Method.getCurrTime();
		String time_start = currTime.substring(8, currTime.length());
		String strRandom = Method.buildRandom(4) + "";
		nonce_str = time_start + strRandom; // 随机数生成算法

		body = "得利库克收银系统";// 简单描述
		detail = "";// 单品优惠功能字段
		attach = "";// 附加数据，在查询API和支付通知中原样返回，该字段主要用于商户携带订单的自定义数据

		String time = currTime.substring(0, currTime.length());
		out_trade_no = time + strRandom;// 日期+4位随机数
										// 商户系统内部订单号，要求32个字符内，只能是数字、大小写字母_-|*@
										// ，且在同一个商户号下唯一

		total_fee = pd.getTotal_fee();// 单位是分
		fee_type = "CNY";// 货币类型
		spbill_create_ip = Method.localIp();
		goods_tag = "";// 订单优惠标记
		auth_code = pd.getAuth_code() == null ? "" : pd.getAuth_code();
		limit_pay = "";// no_credit--指定不能使用信用卡支付
		sub_mch_id = pd.getSub_mch_id() == null ? "" : pd.getSub_mch_id();
		String MICROPAY_URL = "https://api.mch.weixin.qq.com/pay/micropay";

		Map<Object, String> map = new TreeMap<Object, String>();
		map.put("appid", appid);
		map.put("sub_appid", sub_appid);
		map.put("mch_id", mch_id);
		map.put("device_info", device_info);
		map.put("nonce_str", nonce_str);
		map.put("body", body);
		// map.put("detail", detail);
		map.put("attach", attach);
		map.put("out_trade_no", out_trade_no);
		map.put("fee_type", fee_type);
		map.put("total_fee", total_fee);
		map.put("spbill_create_ip", spbill_create_ip);
		map.put("auth_code", auth_code);
		// map.put("goods_tag", goods_tag);
		map.put("sub_mch_id", sub_mch_id);
		sign = Method.createSign("UTF-8", map, key);
		map.put("sign", sign);

		String requestXML = Method.getRequestXml(map);
		Map map1 = null;

		try {
			String resXml = HttpUtil.postData(MICROPAY_URL, requestXML);
			System.out.println("resXml：" + resXml.toString());

			map1 = XMLUtil.doXMLParse(resXml);
			System.out.println("resXml：" + map1.toString());

		} catch (ConnectException ce) {
			System.out.println("连接超时：" + ce.getMessage());
		} catch (Exception e) {
			System.out.println("https请求异常：" + e.getMessage());
		}

		/*
		 * System.out.println("SCAN requestXML::::" + requestXML);
		 * System.out.println("SCAN resXml::::" +
		 * HttpUtil.postData(MICROPAY_URL, requestXML));
		 */

		QueryDao qd = new QueryDao();
		qd.setAppid(appid);
		qd.setMch_id(mch_id);
		qd.setOut_trade_no(out_trade_no);
		qd.setSub_mch_id(sub_mch_id);
		qd.setNonce_str(nonce_str);

		int querycount = 15;
		boolean booleanquery = false;
		while (querycount > 0) {
			if (wechatquery.orderquery(qd) != true && querycount > 0) {
				querycount = querycount - 1;
				booleanquery = false;
			} else {
				querycount = 0;
				booleanquery = true;
			}
			Thread.sleep(1000);
		}
		if (booleanquery == false && querycount == 0) {
			wechatcancel.ordercancel(qd);
			return false;
		}

		return true;
	}

}
